<?php

	if(!isset($_SESSION['username'])) {
		header("Location: ./?page=login");
		die();
	}

	require_once("lib/utility.php");


	if (isset($_REQUEST["run"])) {
		require_once("query_includes.php");
		require_once("lib/class.Template.php");

		$do = false;
		if (isset($_REQUEST["do"])) {
			if ($_REQUEST["do"] == "1") {
				$do = true;
			} else {
				$do = false;
			}
		}

		$templateObj = new Template($_SESSION['username'], $_REQUEST["run"]);
		$variableLookup = $templateObj->getVariableLookup();

		if ((sizeof($variableLookup) == 0) || $do) {

			$overrideMap = array();
			foreach ($variableLookup as $varName=>$varArr) {
				if (isset($_REQUEST[$varName])) {
					$overrideMap[$varName] = $_REQUEST[$varName];
				}
			}

			$templateObj->run($overrideMap);

			//Redirect back to dashboard
			header("Location: ./");
		} else {
			//Allow user to override default variable values

			header("Location: ./?page=template_set_variables&template=".$_REQUEST["run"]);
		}
	}


?>
